package com.bah.tract.client.ui;

import java.util.HashMap;
import java.util.Map;

import com.bah.tract.client.ErrorHandler;
import com.bah.tract.service.Project;
import com.bah.tract.service.ProjectList;
import com.bah.tract.service.ProjectService;
import com.bah.tract.service.TractServiceException_Exception;
import com.bah.tract.service.WorkStore;
import com.bah.tract.service.WorkStoreList;
import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class LabelService {
	private final ProjectService projectService;

	private final Map<Long, String> projectLabels = new HashMap<Long, String>();
	private final Map<Long, String> workStoreLabels = new HashMap<Long, String>();

	@Inject
	public LabelService(final ProjectService projectService) {
		this.projectService = projectService;
	}

	public final String getWorkStoreLabel(final long storeId, final long projectId) {
		if (!workStoreLabels.containsKey(storeId)) {
			try {
				final WorkStoreList workStores = projectService
						.getProjectStores(projectId);
				for (final WorkStore store : workStores.getWorkStore()) {
					workStoreLabels.put(store.getId(), store.getLabel());
				}
			} catch (final TractServiceException_Exception e) {
				ErrorHandler.handleError(e);
			}
		}
		return workStoreLabels.get(storeId);
	}

	public final String getProjectLabel(final long id) {
		if (!projectLabels.containsKey(id)) {
			try {
				final ProjectList projects = projectService.getAllProjects();
				for (final Project project : projects.getProject()) {
					projectLabels.put(project.getId(), project.getLabel());
				}
			} catch (final TractServiceException_Exception e) {
				ErrorHandler.handleError(e);
			}
		}
		return projectLabels.get(id);
	}
}
